import java.util.ArrayList;

public class ProcessMutualFund {
	public static ArrayList<MutualFundOrder> mutualFundOrderList = new ArrayList<MutualFundOrder>();

	public static void processMutualFundOrders() {
		int quantity;
		double mfValue;
		double orderPrice = 0;
		for (MutualFundOrder mutualFundOrder : mutualFundOrderList) {
			quantity = mutualFundOrder.getQuantity();
			int mutualFundId = mutualFundOrder.getMutualFundId();
			mfValue = MutualFundRepository.getMutualFundRepository()
					.getMutualFundPrice(mutualFundId);
			orderPrice = quantity * mfValue;

			int accountId = mutualFundOrder.getAccountId();
			Account account = PortfolioRepository.getPortfolioRepository()
					.getAccount(accountId);
			if (account.getBalance() > orderPrice) {
				double balance = account.getBalance() - orderPrice;
				account.setBalance(balance);
			}

			MutualFund mutualFund = MutualFundRepository
					.getMutualFundRepository().getMutualFund(mutualFundId);
			int companyPortfolioId = mutualFund.getPortfolioId();
			Portfolio companyPortfolio = PortfolioRepository
					.getPortfolioRepository().getPortfolioWithID(
							companyPortfolioId);
			Account companyAccount = companyPortfolio.getAccount(1);
			if (companyAccount != null) {
				if (companyAccount.getBalance() > orderPrice) {
					double balance = companyAccount.getBalance() + orderPrice;
					companyAccount.setBalance(balance);
				}
			}

			mutualFundOrder.closeOrder(true);
		}
	}
}
